<!--
  功能：班主任分配
  作者：licy
  邮箱：licy@briup.com
  时间：2020年12月07日 15:04:22
  版本：v1.0
  修改记录：
  修改内容：
  修改人员：
  修改时间：
-->
<template>
  <div class='clazz_list'>
    <!-- 按钮区域 -->
    <div class="btns">
      <div style="text-align:center">
        <el-form inline size="small">
          <el-form-item>
          <el-select v-model="param.year" placeholder="年度" style="width:100px" clearable>
            <el-option v-for="year in yearOptions" 
              :key="year" 
              :label="year" 
              :value="year">
              </el-option>
          </el-select>
        </el-form-item>
          <el-form-item>
            <el-select v-model="param.directionId" placeholder="请选择方向" clearable>
              <el-option v-for="d in directionData.list" :key="d.id" :label="d.name" :value="d.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="loadClazzs" icon="el-icon-search" type="primary" plain>搜索</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div style="text-align:right">
        <el-button type="primary" size="small" @click="toAddHandler">创建班级</el-button>
        <el-button type="primary" size="small">批量结课</el-button>
      </div>
    </div>
    <!-- 按钮区域结束 -->
    <!-- 表格 -->
    <el-table size="small" :data="clazzData.list" v-loading="loading" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" fixed="left" > </el-table-column>
      <el-table-column label="序号" type="index" :index="1" fixed="left"></el-table-column>
      <el-table-column label="名称" prop="name" width="200" align="center" fixed="left"> </el-table-column>
      <el-table-column label="状态" width="80" align="center" fixed="left"> 
        <template slot-scope="scope">
          <el-tag size="mini" :type="scope.row.status=='正常'?'success':'warning'"> {{scope.row.status}} </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="年度" prop="year" width="60" align="center"> </el-table-column>
      <el-table-column label="基地" prop="area" width="120" align="center"> </el-table-column>
      <el-table-column label="总人数" prop="total" width="60" align="center">
        <template slot-scope="scope">
          <el-tag size="small">{{scope.row.total}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="开班时间" align="center" width="100"> 
        <template slot-scope="scope">
          {{scope.row.beginTime | fmtDate}}
        </template>
      </el-table-column>
      <el-table-column label="结课时间" align="center" width="100"> 
        <template slot-scope="scope">
          {{scope.row.endTime | fmtDate}}
        </template>
      </el-table-column>
      <el-table-column label="班主任" prop="chargeId" width="130" align="center"> 
        <template slot-scope="scope">
          <el-tag size="mini" type="success" v-if="scope.row.charge">{{scope.row.charge.realname}}</el-tag>
          <el-tag size="mini" type="danger" v-else>暂未指定</el-tag>
        </template>
      </el-table-column>
       <el-table-column label="教室"  width="130" align="center"> 
        <template slot-scope="scope">
          <el-tag size="mini" type="success" v-if="scope.row.clazzRoom">{{scope.row.clazzRoom.name}}</el-tag>
          <el-tag size="mini" type="danger" v-else>暂未指定</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="生源" prop="source" align="center" min-width="300"> </el-table-column>
      <el-table-column label="操作" width="100" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="toStartHandler(scope.row)">开班</el-button>
          <el-button type="text" size="mini" @click="toFinishHandler(scope.row)">结课</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格结束 -->
    <!-- 分页 -->
   <div class="page">
      <el-pagination background 
        layout="prev, pager, next,sizes" 
        small
        :total="clazzData.total" 
        :page-size="clazzData.pageSize" 
        :current-page="clazzData.page"
        :page-sizes="[5, 10, 15, 20]"
        @size-change="handleSizeChange"
        @current-change="currentChangeHandler">
      </el-pagination>
   </div>
    <!-- 分页 -->
    <!-- 班级抽屉 -->
    <Briupdrawer :drawer-visible="visible_clazz" title="新增班级" width="40%" @on-change-visible="handlerClazzVisibleChange" >
      <div slot="content">
        <el-form :model="form_clazz" ref="clazzForm" :rules="rules_clazz" label-width="80px" size="mini">
          <el-form-item label="名称" prop="name">
            <el-input v-model="form_clazz.name"></el-input>
          </el-form-item>
          <el-form-item label="方向" prop="directionId">
            <el-select v-model="form_clazz.directionId" placeholder="请选择方向" clearable>
              <el-option v-for="d in directionData.list" :key="d.id" :label="d.name" :value="d.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="年度" prop="year">
            <el-select v-model="form_clazz.year">
              <el-option v-for="year in yearOptions" 
              :key="year" 
              :label="year" 
              :value="year">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="介绍" prop="introduce">
            <el-input type="textarea" v-model="form_clazz.introduce"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible_clazz = false">取 消</el-button>
        <el-button type="primary" size="small" @click="submitClazzFormHandler">确 定</el-button>
      </span>
    </Briupdrawer>
    <!-- 班级抽屉 -->
    <!-- 开班抽屉 -->
    <Briupdrawer :drawer-visible="visible" :title="title" width="40%" @on-change-visible="handlerVisibleChange" >
      <div slot="content">
        <el-form :model="form" ref="clazzForm" :rules="rules" label-width="80px" size="mini">
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="班主任" prop="chargeId">
            <el-select v-model="form.chargeId">
              <el-option v-for="t in teacherData.list" :label="t.realname" :value="t.id" :key="t.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="基地" prop="area">
            <el-select v-model="form.area">
              <el-option label="校内" value="校内" ></el-option>
              <el-option label="鳗鲤湖基地" value="鳗鲤湖基地" ></el-option>
              <el-option label="浦东软件园基地" value="浦东软件园基地" ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="教室" prop="roomId">
            <el-select v-model="form.roomId">
              <el-option v-for="t in clazzRoomData.list" :label="t.name" :value="t.id" :key="t.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="人数" prop="total">
            <el-input v-model="form.total"></el-input>
          </el-form-item>
          <el-form-item label="开班时间" prop="beginTime">
            <el-date-picker v-model="form.beginTime" value-format="timestamp" type="date" placeholder="选择日期" />
          </el-form-item>
          <el-form-item label="结课时间" prop="endTime">
            <el-date-picker v-model="form.endTime" value-format="timestamp" type="date" placeholder="选择日期" />
          </el-form-item>
          <el-form-item label="生源" prop="source">
            <el-input type="textarea" v-model="form.source" :rows="4"></el-input>
          </el-form-item>
          <el-form-item label="介绍" prop="introduce">
            <el-input type="textarea" v-model="form.introduce" :rows="4"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible = false">取消</el-button>
        <el-button size="small" type="primary" @click="submitHandler">确定</el-button>
      </span>
    </Briupdrawer>
    <!-- 抽屉结束 -->
  </div>
</template>

<script>
import {get,post} from '@/utils/request'
import _ from 'lodash'
import { generateYearOptions } from '@/utils/year' // 获取年份选项
export default {
  // 组件名称
  name: 'demo',
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data () {
   return {
    visible:false,
    visible_clazz:false,
    loading:false,
    title:"",
    clazzs:[],  //选择到的班级
    param:{
      page:1,
      pageSize:15,
      year: '' 
    },
    yearOptions: [],  // 年份选项
    clazzData:{ list:[] },
    teacherData:{ list:[] },
    clazzRoomData:{list:[]},
    directionData:{list:[]},
    fileList:[],
    form:{},
    form_clazz:{},
    rules:{
      chargeId: [ { required: true, message: '请选择班主任', trigger: 'change' } ],
      area: [ { required: true, message: '请输入基地', trigger: 'change' } ],
      roomId: [ { required: true, message: '请选择教室', trigger: 'change' } ],
      total:[{ required: true, message: '请输入总人数', trigger: 'blur' } ],
      beginTime:[{ required: true, message: '请选择开班时间', trigger: 'change' } ],
      endTime:[{ required: true, message: '请选择结课时间', trigger: 'change' } ],
      source:[{ required: true, message: '请输入生源', trigger: 'blur' } ]
    },
    rules_clazz:{
        name: [ { required: true, message: '请输入名称', trigger: 'blur' } ],
        introduce: [ { required: true, message: '请输入简介', trigger: 'blur' } ],
        year: [ { required: true, message: '请选择年度', trigger: 'change' } ],
        directionId: [ { required: true, message: '请选择年度', trigger: 'change' } ],
      },
   }
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  // 组件方法
  methods: {
    // 保存班级
    submitClazzFormHandler(){
    // 表单校验
      this.$refs['clazzForm'].validate((valid) => {
        if (valid) {
          // 校验通过后，完成数据交互
          let url = '/clazz/saveOrUpdate'
          post(url,this.form_clazz).then(response => {
            this.$message({type:"success",message:response.message})
            this.loadClazzs();
            this.visible_clazz = false;
          })
        } else {
          return false;
        }
      });
    },
    toAddHandler(){
      this.form_clazz = {};
      // 显示抽屉
      this.visible_clazz = true;
    },
    handleSelectionChange(val) {
      this.clazzs = val;
    },
    // 上传成功
    uploadSuccessHandler(response){
      if(response.status == 200){
        this.$set(this.form,'photo',response.data.id);
      } else {
        this.$message({type:'error',message:'附件服务器异常！'})
        this.visible = false;
      }
    },
    // 确认提交处理函数
    submitHandler(){
      // 表单校验
      this.$refs['clazzForm'].validate((valid) => {
        if (valid) {
          // 校验通过后，完成开班
          let url = '/clazz/startClazz'
          post(url,this.form).then(response => {
            this.$message({type:"success",message:response.message})
            this.loadClazzs();
            this.visible = false;
          })
        } else {
          return false;
        }
      });
    },
    toFinishHandler(row){
      this.$confirm('是否确认结班?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = "/clazz/finishClazz"
        get(url,{id:row.id})
        .then(response => {
          this.$message({ type: 'success', message: response.message })
          this.loadClazzs()
        })
      })
    },
    // 去修改
    toStartHandler(row){
      // 修改抽屉标题
      this.title = "修改班级信息"
      // 将当前行数据的克隆对象赋值给当前form,这样可以避免修改表单数据导致表格数据发生变化。
      this.form = _.clone(row);
      // 显示抽屉
      this.visible = true;
    },
    // 查询班级信息
    loadClazzs(){
      // 添加loading,页面加载开始
      this.loading = true;
      let url = "/clazz/pageQueryFull"
      for(let k in this.param){
        if(!this.param[k]){
          delete this.param[k]
        }
      }
      get(url,this.param).then(response => {
        this.clazzData = response.data;
        // 页面加载结束
        this.loading = false;
      })
    },
     // 查询方向信息
    loadDirections(){
      // 添加loading,页面加载开始
      this.loading = true;
      let url = "/direction/pageQuery"
      get(url,{page:1,pageSize:100}).then(response => {
        this.directionData = response.data;
        // 页面加载结束
        this.loading = false;
      })
    },
    // 查询班主任老师
    loadTeachers(){
      let url = "/employee/pageQuery"
      get(url,{
        page:1,
        pageSize:100,
        deptId:2
      }).then(response => {
        this.teacherData = response.data;
      })
    },

     // 查询教室信息
    loadClazzRooms(){
      let url = "/clazz_room/pageQuery"
      get(url,{
        page:1,pageSize:100
      }).then(response => {
        this.clazzRoomData = response.data;
      })
    },
    // 当前页发生改变
    currentChangeHandler(page){
      this.param.page = page;
      this.loadClazzs();
    },
    // 处理分页大小变化的函数
    handleSizeChange(pageSize){
      // 当前页为第一页
      this.page = 1;
      // 页面大小为用户选择的页面
      this.param.pageSize = pageSize;
      this.loadClazzs();
    },
    // 重置表单校验
    clearValidate() {
      this.$refs['clazzForm'].clearValidate();
    },
    // 处理visible变化的钩子函数，解决抽屉无法收缩的bug
    handlerVisibleChange(val) {
      this.visible = val
    },
    handlerClazzVisibleChange(val){
      this.visible_clazz = val;
    }
  },
  // 以下是生命周期钩子
  /**
  * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
  */
  created () {
    // 加载班级信息
    this.loadClazzs();
    // 加载方向信息
    this.loadDirections();
    // 查询班主任信息
    this.loadTeachers();
    // 查询教室
    this.loadClazzRooms();
  },
  /**
  * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调用该钩子。
  * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.$ el 也在文档内。
  */
  mounted () {
    this.yearOptions = generateYearOptions();
  },
  /**
  * 实例销毁之前调用。在这一步，实例仍然完全可用。
  */
  beforeDestroy () {
  },
  /**
  * Vue 实例销毁后调用。调用后，Vue 实例指示的所有东西都会解绑定，
  * 所有的事件监听器会被移除，所有的子实例也会被销毁。
  */
  destroyed () {
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--使用了scoped属性之后，父组件的style样式将不会渗透到子组件中，-->
<!--然而子组件的根节点元素会同时被设置了scoped的父css样式和设置了scoped的子css样式影响，-->
<!--这么设计的目的是父组件可以对子组件根元素进行布局。-->
<style scoped>

</style>

